package cn.com.zj.rpc.factory;

import cn.com.zj.rpc.api.IUserService;
import cn.com.zj.rpc.client.MyRpcClient;
import cn.com.zj.rpc.config.ServerAddressConfig;
import cn.com.zj.rpc.proxy.MyRpcProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName UserServiceFactory
 * @Description UserServiceImpl工场
 * @Author ZikZ227412
 * @DATE 2021/12/10 14:48
 * @Version 1.0
 */
@Component
public class UserServiceFactory {


    @Autowired
    ServerAddressConfig serverAddressConfig;

    //计数器
    private Integer count = 0;

    //自定义负载轮询器，轮序服务端地址
    public MyRpcClient getMyRpcClient() {
        List<Map> serverList = serverAddressConfig.getLists();
        //当计数器等于集合大小时，将计数器重置为0
        if (count == serverList.size()) {
            count = 0;
        }
        Map map = serverList.get(count);
        count++;
        return new MyRpcClient(map.get("ip").toString(), Integer.parseInt(map.get("port").toString()));
    }


    /**
     * 功能描述:获取IUserService实现类
     *
     * @param
     * @return cn.com.zj.rpc.api.IUserService
     * @author zj
     * @date 2021/12/10
     */
    public IUserService getUserService() {

        return (IUserService) MyRpcProxy.createProxy(IUserService.class, getMyRpcClient());
    }
}
